package com.woniu.user.service.Imp;

import com.woniu.common.pojo.Ping;
import com.woniu.user.mapper.PingMapper;
import com.woniu.user.service.PingService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PingServiceImpl implements PingService {
    @Resource
    private PingMapper pingMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Ping> selectAllPing() {
        //从redis中获取ping
        List<Ping> pingList = (List<Ping>) redisTemplate.opsForValue().get("pingList");
        if (pingList == null) {
            //从数据库中获取ping
            System.out.println("从数据库中获取ping");
            pingList = pingMapper.selectAllPing();
            //将ping存入redis
            redisTemplate.opsForValue().set("pingList", pingList);
        }
        System.out.println("从redis中获取ping");
        return pingList;

//        return pingMapper.selectAllPing();
    }

    @Override
    public void addPing(Ping ping) {
        // 添加评论到数据库
        pingMapper.addPing(ping);

        // 从 Redis 中删除现有的 pingList，以便下次查询时重新加载最新的数据
        redisTemplate.delete("pingList");

        System.out.println("添加新评论后，已从 Redis 中删除旧的 pingList");
    }
}
